package com.canaan.business.service.storehouse.impl;

import java.util.Date;
import java.util.List;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.TableNoUtil;
import com.canaan.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.canaan.business.mapper.storehouse.TacticConfigMapper;
import com.canaan.business.domain.storehouse.TacticConfig;
import com.canaan.business.service.storehouse.ITacticConfigService;

/**
 * 库存配置策略Service业务层处理
 *
 * @author ruoyi
 * @date 2023-02-24
 */
@Service
public class TacticConfigServiceImpl implements ITacticConfigService {
    @Resource
    private TacticConfigMapper tacticConfigMapper;

    /**
     * 查询库存配置策略
     *
     * @param id 库存配置策略主键
     * @return 库存配置策略
     */
    @Override
    public TacticConfig selectTacticConfigById(Long id) {
        return tacticConfigMapper.selectTacticConfigById(id);
    }

    /**
     * 查询库存配置策略列表
     *
     * @param tacticConfig 库存配置策略
     * @return 库存配置策略
     */
    @Override
    public List<TacticConfig> selectTacticConfigList(TacticConfig tacticConfig) {
        return tacticConfigMapper.selectTacticConfigList(tacticConfig);
    }

    /**
     * 新增库存配置策略
     *
     * @param tacticConfig 库存配置策略
     * @return 结果
     */
    @Override
    public int insertTacticConfig(TacticConfig tacticConfig) {
        if(StringUtils.isBlank(tacticConfig.getNo())){
            tacticConfig.setNo(TableNoUtil.getNo(TacticConfig.class));
        }
        tacticConfig.setCreateBy(SecurityUtils.getUsername());
        tacticConfig.setCreateTime(DateUtils.getNowDate());
        return tacticConfigMapper.insertTacticConfig(tacticConfig);
    }

    /**
     * 批量新增库存配置策略
     *
     * @param tacticConfigList 库存配置策略列表
     * @return 结果
     */
    @Override
    public int batchInsertTacticConfig(List<TacticConfig> tacticConfigList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (TacticConfig tacticConfig : tacticConfigList){
            if(StringUtils.isBlank(tacticConfig.getNo())){
                tacticConfig.setNo(TableNoUtil.getNo(TacticConfig.class));
            }
            tacticConfig.setCreateBy(username);
            tacticConfig.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<tacticConfigList.size();) {
            int endIndex = i+500;
            if (endIndex > tacticConfigList.size()) {
                endIndex = tacticConfigList.size();
            }
            insertNum = insertNum + tacticConfigMapper.batchInsertTacticConfig(tacticConfigList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改库存配置策略
     *
     * @param tacticConfig 库存配置策略
     * @return 结果
     */
    @Override
    public int updateTacticConfig(TacticConfig tacticConfig) {
        tacticConfig.setUpdateBy(SecurityUtils.getUsername());
        tacticConfig.setUpdateTime(DateUtils.getNowDate());
        return tacticConfigMapper.updateTacticConfig(tacticConfig);
    }

    /**
     * 批量修改库存配置策略
     *
     * @param tacticConfigList 库存配置策略列表
     * @return 结果
     */
    @Override
    public int batchUpdateTacticConfig(List<TacticConfig> tacticConfigList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (TacticConfig tacticConfig : tacticConfigList){
            tacticConfig.setUpdateBy(username);
            tacticConfig.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<tacticConfigList.size();) {
            int endIndex = i+500;
            if (endIndex > tacticConfigList.size()) {
                endIndex = tacticConfigList.size();
            }
            updateNum = updateNum + tacticConfigMapper.batchUpdateTacticConfig(tacticConfigList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除库存配置策略
     *
     * @param ids 需要删除的库存配置策略主键集合
     * @return 结果
     */
    @Override
    public int deleteTacticConfigByIds(Long[] ids) {
        return tacticConfigMapper.deleteTacticConfigByIds(ids);
    }

    /**
     * 删除库存配置策略信息
     *
     * @param id 库存配置策略主键
     * @return 结果
     */
    @Override
    public int deleteTacticConfigById(Long id) {
        return tacticConfigMapper.deleteTacticConfigById(id);
    }

    /**
     * 查询库存配置策略
     *
     * @param no 编号
     * @return 库存配置策略
     */
    @Override
    public TacticConfig selectTacticConfigByNo(String no) {
        return tacticConfigMapper.selectTacticConfigByNo(no);
    }

    /**
     * 批量删除库存配置策略
     *
     * @param nos 需要删除的编号集合
     * @return 结果
     */
    @Override
    public int deleteTacticConfigByNos(String[] nos) {
        return tacticConfigMapper.deleteTacticConfigByNos(nos);
    }

    /**
     * 删除库存配置策略信息
     *
     * @param no 编号
     * @return 结果
     */
    @Override
    public int deleteTacticConfigByNo(String no) {
        return tacticConfigMapper.deleteTacticConfigByNo(no);
    }
}
